//
//  JAStatusPictureView.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/6.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit

fileprivate let baseTag = 666

class JAStatusPictureView: UIView {

    //视图模型
    var statusViewModel: JAStatusViewModel? {
        didSet {
            //imageView是否需要显示.由数据决定
            //先将所有的图片隐藏
            for i in 0..<9 {
                
                let imageView = self.viewWithTag(i + baseTag)
                imageView?.isHidden = true
                
            }
            
            //根据数据决定是否需要显示imageView
            if let pic_urls = statusViewModel?.pic_urls, pic_urls.count > 0 {
                var index = 0
                for pictureModel in pic_urls {
                    //先取到对应的index的imgaeView
                    let imageView = self.viewWithTag(index + baseTag) as? UIImageView
                    imageView?.isHidden = false
                
                    imageView?.wb_setImage(urlStr: pictureModel.thumbnail_pic!, placeHoder: "avatar_default_big")
                    
                    //处理单张图片
                    //如果是第一张图片, 并且有且只有一张图片, 则让图片等比例显示
                    if index == 0 && pic_urls.count == 1 {
                        imageView?.frame = CGRect(origin: CGPoint.zero, size:
                        (statusViewModel?.pictureViewSize)!)
                    }
                    //如果是第一张图片, 但是不止一张图片, 图片的宽高就按照九宫格的正方形大小显示
                    else if index == 0 && pic_urls.count > 1 {
                        let imageWH = (screenWidh - 40)/3
                        imageView?.frame = CGRect(x: 0, y: 0, width: imageWH, height: imageWH)
                    }
            
                   
                    //四张图片的处理
                    if pic_urls.count == 4 && index == 1 {
                        index += 1
                    }
                    
                    index += 1
                }
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension JAStatusPictureView  {
    fileprivate func setupUI () {
        //如果子视图超出父视的frame, 则将超出的部分切掉
        self.clipsToBounds = true
        self.backgroundColor = UIColor.white
        
        //创建九个imageView
        let imageWH = (screenWidh - 40)/3
        
        //移动一次,需要调整水平或者垂直的间隔
        let gap = imageWH + 10
        let firstImageFrame = CGRect(x: 0, y: 0, width: imageWH, height: imageWH)
        for i in 0..<9 {
            let imageView = UIImageView()
            imageView.backgroundColor = UIColor.red
            addSubview(imageView)
            
            imageView.tag = i + baseTag
            
            //获得行数和列数
            let row = i / 3
            let col = i % 3
            
            //计算每个imageView的frame
            let frame = firstImageFrame.offsetBy(dx: CGFloat(col)*gap, dy: CGFloat(row)*gap)
            
            imageView.frame = frame
            
            //.scaleAspectFit 图片适应容器,图片会完全显示, 但是imageView会显示空白
            //. scaleAspectFill 图片会完全填满imageView,但是可能超出imageView
             imageView.contentMode = .scaleAspectFill
             imageView.clipsToBounds = true
            
            //给imageView添加点击事件
            imageView.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(imageClicked(tap:)))
            imageView.addGestureRecognizer(tap)
        }
    }
}

// MARK: - 事件处理
extension JAStatusPictureView {
    @objc func imageClicked(tap: UITapGestureRecognizer) {
     
        //需要将当前的配图视图的所有图片的url数组和被点击的图片的index, 传给图片浏览控制器
        var index = tap.view!.tag - baseTag
        
        //如果有四张图片, 图片是2*2显示的, 当到第三张图片时, 应该将index - 1
        if  let pic_urls = statusViewModel?.pic_urls, pic_urls.count == 4, index > 2 {
            index -= 1
        }
        
        //获取所有的url的字符串数组
        //从一个模型数组中, 取到属性数组; 这是oc中的类有相关的api, 而swift的数组没有这个api
        let urlStrs = ((statusViewModel?.pic_urls)! as NSArray).value(forKeyPath: "middle_pic") as! [String]
        
        let userInfo: [String: Any] = ["index": index, "urls": urlStrs]

        //创建通知
        let notification = Notification(name: pictureViewClickedNotification, object: nil, userInfo: userInfo)
        
        //发送通知
        NotificationCenter.default.post(notification)
    }
}
